<?php

namespace App\Controller\Common;

use App\Base\BaseConfig;
use App\Base\BaseController;
use App\Base\BaseModel;
use BaseExt\JPush;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;


/**
 * @route("/appcan")
 */
class AppcanController extends BaseController
{
    public function initData()
    {
        $request = Request::createFromGlobals();
        $url = $request->getBaseUrl();
        if (strpos($url,"pp_dev")>0) {
            $env = "dev";
            $data["rnd"] = "?rnd=".date("YmdHis");
//            $data["rnd"] = "?rnd=".date("YmdH");
            $data["asset"] = BaseConfig::__BASE_URL__;
            $data["root"] = BaseConfig::__BASE_URL__;
        } else {
            $env = "prod";
            $data["rnd"] = "?v=1.07";
            $data["asset"] = "http://sale.assets.sano100.com/ac/dc";
            $data["root"] = BaseConfig::__BASE_URL__;
        }
        $data["env"] = $env;
        $data["defaultPic"] = $data["asset"]."/assets/default.png";
        return $data;
    }

    /**
     * @route("/page",name="AppCanCommentPage")
     */
    public function AppCanCommentPage(){
        if(!$PageName = $this->getParamRequired("PageName")){die("<h1>ERROR!</h1>");}
        if(!$ClientName = $this->getParamRequired("ClientName")){die("<h1>ERROR!</h1>");}
        $data = $this->initData();
        $data["PageName"] = "$PageName";
        $data["Client"] = $ClientName;
        $Client = ucfirst($ClientName);
        $tabPage =array("slider","indexTab","index","orders","userHome");
        if (in_array($PageName,$tabPage)) {$data["istab"] = "1";} else {$data["istab"] = "0";}
        try {
            return $this->render("AppBundle:$Client:$PageName.html.twig",$data);
        } catch (\Exception $e) {
            die("<h2 style='text-align: center;'>技术小哥哥<br>正在努力……<br>努力赚加班费……<br>(￥ω￥)</h2>".$e->getMessage());
        }
    }

//    /**
//     * @route("/user/page",name="AppCanUserCommentPage")
//     */
//    public function AppCanUserCommentPage(){
//        if(!$PageName = $this->getParamRequired("PageName")){die("<h1>ERROR!</h1>");}
//        $data = $this->initData();
//        $data["PageName"] = "$PageName";
//        $data["Client"] = "user";
//        $tabPage =array("slider","indexTab","index","orders","userHome");
//
//        if (in_array($PageName,$tabPage)) {$data["istab"] = "1";} else {$data["istab"] = "0";}
//
//        try {
//            return $this->render("AppBundle:User:$PageName.html.twig",$data);
//        } catch (\Exception $e) {
//            die("<h2 style='text-align: center;'>技术小哥哥<br>正在努力……<br>努力赚加班费……<br>(￥ω￥)</h2>");
//        }
//    }
//    /**
//     * @route("/shop/page",name="AppCanShopCommentPage")
//     */
//    public function AppCanShopCommentPage(){
//        if(!$PageName = $this->getParamRequired("PageName")){die("<h1>ERROR!</h1>");}
//        $data = $this->initData();
//        $data["PageName"] = "$PageName";
//        $data["Client"] = "shop";
//        $tabPage =array("slider","indexTab","index","orders","userHome");
//        if (in_array($PageName,$tabPage)) {$data["istab"] = "1";} else {$data["istab"] = "0";}
//
//        try {
//            return $this->render("AppBundle:Shop:$PageName.html.twig",$data);
//        } catch (\Exception $e) {
//            die("<h2 style='text-align: center;'>技术小哥哥<br>正在努力……<br>努力赚加班费……<br>(￥ω￥)</h2>".$e->getMessage());
//        }
//    }
//
//
//    /**
//     * @route("/test",name="AppCanTest")
//     */
//    public function AppCanTest(){
//        $data = $this->initData();
//
////        $JP = new JPush();
////        $JP->pushToOneUser(15,"titiel".time(),"content".time());
//
////        $data["PageName"] = "$PageName";
////        $data["Client"] = "shop";
////        $tabPage =array("slider","indexTab","index","orders","userHome");
////
////        if (in_array($PageName,$tabPage)) {$data["istab"] = "1";} else {$data["istab"] = "0";}
//        return $this->render("AppBundle::test.html.twig",$data);
//    }


    /**
     * @route("/AppCanGenerateLocalFile",name="AppCanGenerateLocalFile")
     */
    public function AppCanGenerateLocalFile()
    {
        $UserFiles = "feedBack.html.twig
help.html.twig
index.html.twig
indexTab.html.twig
orders.html.twig
setting.html.twig
share.html.twig
shopGoods.html.twig
shopGoodsEdit.html.twig
shopInfo.html.twig
shopInfoSetAddress.html.twig
shopInfoSetIntro.html.twig
shopInfoSetName.html.twig
shopInfoSetPic.html.twig
slider.html.twig
userHome.html.twig
userLogin.html.twig
userMessage.html.twig
userPassword.html.twig
userReg.html.twig";
        $pages = mb_split("\n",$UserFiles);
//        var_dump($pages);


        $data["env"] = "dev";
        $data["rnd"] = "?rnd=".date("YmdHis");
//            $data["rnd"] = "?rnd=".date("YmdH");
        $data["asset"] = "../..";
        $data["root"] = "../..";
        $data["Client"] = "shop";
        $data["defaultPic"] = $data["asset"]."/assets/default.png";

        foreach ($pages as $fileName) {
            $PageName =  substr($fileName,0,-10);
            $data["PageName"] = "$PageName";
            $tabPage =array("slider","indexTab","index","orders","userHome");
            if (in_array($PageName,$tabPage)) {$data["istab"] = "1";} else {$data["istab"] = "0";}
            $HtmlName = substr($fileName,0,-5);
            $client = ucfirst($data["Client"]);
            $html = $this->renderView("AppBundle:$client:$fileName",$data);

            //检查目录是否存在
            if (!is_dir("./tmp/".$data["Client"] )) {
                mkdir("./tmp/".$data["Client"] );
            }
            if (!file_exists("./tmp/".$data["Client"] ."/".$HtmlName)) {

            }

            file_put_contents("./tmp/".$data["Client"] ."/".$HtmlName,$html);
            var_dump($HtmlName);
        }


    }

}
